﻿using System;
using Autofac;
using ComputerVision.BaseForm;
using ComputerVision.Common;
using ComputerVision.Data;
using ComputerVision.PhieuDieuTraCoSoForm;
using ComputerVision.Services;
using ComputerVision.Services.User;
using QuanLyThongTinCoSo.Common;

namespace ComputerVision.CoQuanQuanLyPCCCForm
{
    public partial class frmCoQuanQuanLyPCCCDetail : frmDetail
    {
        private readonly IService<CoQuanQuanLyPCCC> _coQuanQuanLyPCCCService;
        private frmPhieuDieuTraCoSoDetail _frmPhieuDieuTraCs;
        private readonly IUserService<aspnet_Users> _userService;
        public frmCoQuanQuanLyPCCCDetail()
        {
            InitializeComponent();
            _coQuanQuanLyPCCCService = Program.Container.Resolve<IService<CoQuanQuanLyPCCC>>();
            _userService = Program.Container.Resolve<IUserService<aspnet_Users>>();
            Text = @"Thêm mới Cơ quan quản lý";
            txtTenCoQuan.Focus();
        }

        public frmCoQuanQuanLyPCCCDetail(frmPhieuDieuTraCoSoDetail frm)
        {
            InitializeComponent();
            this._frmPhieuDieuTraCs = frm;
            _coQuanQuanLyPCCCService = Program.Container.Resolve<IService<CoQuanQuanLyPCCC>>();
            _userService = Program.Container.Resolve<IUserService<aspnet_Users>>();
            Text = @"Thêm mới Cơ quan quản lý";
            txtTenCoQuan.Focus();
        }
        
        public frmCoQuanQuanLyPCCCDetail(Guid id)
        {
            InitializeComponent();
            _coQuanQuanLyPCCCService = Program.Container.Resolve<IService<CoQuanQuanLyPCCC>>();
            _userService = Program.Container.Resolve<IUserService<aspnet_Users>>();
            Text = @"Sửa Ngành";
            txtTenCoQuan.Focus();

            _fromtype = Constants.TypeForm.Edit;
            guid = id;

            var item = _coQuanQuanLyPCCCService.Detail(id);
            if (item != null)
            {
                txtTenCoQuan.Text = item.Ten;
                txtDienGiai.Text = item.DienGiai;
            }
        }

        #region Validation

        protected override bool Validator()
        {
            bool flag = true;

            errorProvider.Dispose();

            if (string.IsNullOrEmpty(txtTenCoQuan.Text))
            {
                errorProvider.SetError(txtTenCoQuan, "Bạn chưa nhập tên cơ quan quản lý. Xin vui lòng kiểm tra lại");
                flag = false;
            }
            
            return flag;
        }

        #endregion

        #region Insert
        protected override void Insert()
        {
            try
            {
                var model = new CoQuanQuanLyPCCC
                {
                    Id = Guid.NewGuid(),
                    Ten = txtTenCoQuan.Text,
                    DienGiai = txtDienGiai.Text,
                    Created = DateTime.Now,
                    CreatedBy_Id = Constants.CurrentUser,
                    Modified = DateTime.Now,
                    ModifiedBy_Id = Constants.CurrentUser
                };
                _coQuanQuanLyPCCCService.Create(model);

                if (this._frmPhieuDieuTraCs != null)
                {
                    this._frmPhieuDieuTraCs.LoadCoQuanQl();
                }
            }
            catch (Exception ex)
            {
                AlertList.Error("Lỗi phát sinh khi tạo mới dữ liệu.");
            }
        }
        #endregion

        #region Edit

        protected override void Edit()
        {
            var item = _coQuanQuanLyPCCCService.Detail(guid);
            if (item != null)
            {
                item.Ten = txtTenCoQuan.Text;
                item.DienGiai = txtDienGiai.Text;
                item.Modified = DateTime.Now;
                item.ModifiedBy_Id = Constants.CurrentUser;
                _coQuanQuanLyPCCCService.Update(item);
            }
        }

        #endregion
    }
}
